package com.msmall.community.admin.security.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.msmall.base.util.Constants;
import com.msmall.community.common.OnlineUser;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 检查后台用户是否登陆
 */
public class AdminSecurityInterceptor implements Interceptor{

	private static final long serialVersionUID = 1883202406508271481L;

	public String intercept(ActionInvocation ai) throws Exception {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		Long userid = (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
		if(OnlineUser.getOnlineUsers().get(userid).getRole() == OnlineUser.USER_ROLE_ADMIN){
			return ai.invoke();
		}else{
			return "admin-login";
		}
		
	}

	public void destroy() {
		
	}

	public void init() {
		
	}
}
